/**
 * 
 */
package org.odk.collect.android.cse481k;

import org.odk.collect.android.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * Tiny activity class to retrieve GPS coordinates.<br />
 * It is intended to get destroyed as soon as the first GPS coordinates are acquired. (Apparently it survives) <br />
 * Will probably need to add timeout feature; it just sits and waits <br />
 * CSE481
 * @author Chiyoung
 *
 */
public class CustomGPS extends Activity {
	
	
	/**
	 * Probably not the best way to do it
	 */
	public void onCreate(Bundle savedInstanceState){
		
		//Some initializations
		super.onCreate(savedInstanceState);
		boolean GPS = true;
		
		
		//Sets the activity title bar. In real device, acquiring GPS will take time.
		setTitle(getString(R.string.app_name) + " > " + getString(R.string.get_location));
		
		LocationManager locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener locationlistener = new mLocationListener();
		
		//Only proceed if GPS is ENABLED by the user setting
		if(locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
			try{
				locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10000, locationlistener);
			
				//Retrieve GPS coordinates
				Location m = locationmanager.getLastKnownLocation(locationmanager.getBestProvider(new Criteria(), true));
				
				String gps = "Lat = " + m.getLatitude() +
				" Lon = " + m.getLongitude();
				Log.i("gps", gps);
				
				//Push to the intent that is to be sent back to the caller
				Intent i = new Intent();
				
				i.putExtra("Latitude", m.getLatitude());
				i.putExtra("Longitude", m.getLongitude());
				
				//Finalize the result
				setResult(RESULT_OK, i);
				
				Toast message = Toast.makeText(this, "Current location:\nLatitude: " + m.getLatitude() + "\nLongitude: " + m.getLongitude(), Toast.LENGTH_LONG);
				message.show();
				finish();

				/*
				 * Weird thing is that the service actually lives even after calling finish() - 
				 * mLocationListener's methods still respond to GPS changes. I do not know how to effectively TERMINATE the service.
				 */
				
				
			//Things can go wrong (no sky, no service, no wifi, etc.)
			} catch (Exception e){
				setResult(RESULT_CANCELED, new Intent());
				Toast message = Toast.makeText(this, "GPS went wrong", Toast.LENGTH_LONG);
				message.show();
				finish();
			}
			
		//GPS is DISABLED by the user setting
		} else {
			setResult(RESULT_CANCELED, new Intent());
			Toast message = Toast.makeText(this, "GPS Not Enabled!", Toast.LENGTH_LONG);
			message.show();	
			finish();
		}
		
	}
	
	/**
	 * Dummy class. No useful method is defined
	 * @author Chiyoung
	 *
	 */
	private class mLocationListener implements LocationListener{

		@Override
		public void onLocationChanged(Location location) {
			Log.i("gps", "Movement detected");
		}

		@Override
		public void onProviderDisabled(String provider) {
			Log.i("gps", provider + " not available");	
		}

		@Override
		public void onProviderEnabled(String provider) {
			Log.i("gps", provider + " available");	
			
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			Log.i("gps", provider + " status changed: " + status);
			
		}
		
	}
	

}
